﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Data;
using BookStore.Bussiness;
using BookStore.Common;

namespace BookStore.admin
{
    public partial class Admin : System.Web.UI.Page
    {
        static bool flag=false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
        }
        public void showAll() {
            gvAdmin.DataSource = adminService.getAll();
            gvAdmin.DataBind();
        }
        public void ResetControl()
        {
            txtUsername.Text ="";
            txtPassword.Text ="";
            txtFullname.Text ="";
            txtBirthday.Text = "";
            txtAdd.Text ="";
            txtEmail.Text = "";
            txtDateregister.Text =""; 
        }

        public void insertAdmin() {
            adminInfo objAdmin = new adminInfo();
            if (adminService.checkUsername(txtUsername.Text)==true)
            {
                Response.Write("<script language='javascript'>alert('Username existed');</script>");
                return;
            }
            objAdmin.username = txtUsername.Text;
            objAdmin.password = encryption.toMD5(txtPassword.Text);
            objAdmin.fullname = txtFullname.Text;
            objAdmin.birthday = Convert.ToDateTime(txtBirthday.Text);
            objAdmin.address = txtAdd.Text;
            objAdmin.email = txtEmail.Text;
            objAdmin.registerDate = Convert.ToDateTime(txtDateregister.Text);
            objAdmin.decentralization = Convert.ToInt32(ddlDecen.SelectedValue);
            bool test=adminService.insertAdmin(objAdmin);
            if (test==true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updateAdmin()
        {
            adminInfo objAdmin = new adminInfo();
            objAdmin.username = txtUsername.Text;
            objAdmin.password = encryption.toMD5(txtPassword.Text);
            objAdmin.fullname = txtFullname.Text;
            objAdmin.birthday = Convert.ToDateTime(txtBirthday.Text);
            objAdmin.address = txtAdd.Text;
            objAdmin.email = txtEmail.Text;
            objAdmin.registerDate = Convert.ToDateTime(txtDateregister.Text);
            objAdmin.decentralization = Convert.ToInt32(ddlDecen.SelectedValue);
            bool test = adminService.updateAdmin(objAdmin);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }

        protected void lbtUpdateT_Click(object sender, EventArgs e)
        {
            if (flag==true)
            {
                insertAdmin();
            }
            else
            {
                updateAdmin();
            }
            showAll();
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }


        protected void lbtAddT_Click(object sender, EventArgs e)
        {
            flag = true;
            ResetControl();
            pnView.Visible = false;
            pnUpdate.Visible = true;
        }

        protected void lbtRefreshT_Click(object sender, EventArgs e)
        {
            showAll();
        }

        protected void gvAdmin_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
                string username = gvAdmin.Rows[e.RowIndex].Cells[0].Text;
                if (username.Equals("admin"))
                {
                    Response.Write("<script language='javascript'>alert(' donot delete');</script>");
                    return;   
                }
                bool test=adminService.deleteAdmin(username);
                if (test == true)
                {
                    Response.Write("<script language='javascript'>alert('delete success');</script>");
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('delete failed');</script>");
                }
            showAll();
        }

        protected void gvAdmin_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag = false;
            string username = gvAdmin.Rows[e.NewSelectedIndex].Cells[0].Text;
            adminInfo objAdmin = adminService.getbyID(username);
            txtUsername.Text = objAdmin.username;
            txtPassword.Text = objAdmin.password;
            txtFullname.Text = objAdmin.fullname;
            txtBirthday.Text = objAdmin.birthday.ToString();
            txtAdd.Text = objAdmin.address;
            txtEmail.Text = objAdmin.email;
            txtDateregister.Text = objAdmin.registerDate.ToString();
            ddlDecen.SelectedValue = objAdmin.decentralization.ToString();
            pnView.Visible = false;
            pnUpdate.Visible = true;
        }

        protected void gvAdmin_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAdmin.PageIndex = e.NewPageIndex;
            showAll();
        }

      

    }
}
